<template>
  <div>
    <van-form @submit="onSubmit" ref="form">
      <div class="form-item" v-for="(item,$index) in form" :key="$index">
        <van-field-text
          :type="typeno"
          class="van-field-line hide-box"
          v-if="item.node=='id'"
          :formItem="item"
        ></van-field-text>
        <van-field-text
          :type="typeno"
          class="van-field-line"
          v-if="item.node=='text'"
          :formItem="item"
        ></van-field-text>
        <van-field-select :type="typeno" v-if="item.node=='select'" :formItem="item"></van-field-select>
        <van-field-checkbox
          :type="typeno"
          class="van-field-line"
          v-if="item.node=='checkbox'"
          :formItem="item"
        ></van-field-checkbox>
        <van-field-radio
          :type="typeno"
          class="van-field-line"
          v-if="item.node=='radio'"
          :formItem="item"
        ></van-field-radio>
        <van-field-datetime
          :type="typeno"
          class="van-field-line"
          v-if="item.node=='datetime'"
          :formItem="item"
        ></van-field-datetime>
        <van-field-calendar
          :type="typeno"
          class="van-field-line"
          v-if="item.node=='calendar'"
          :formItem="item"
        ></van-field-calendar>
        <van-field-fileupload
          :type="typeno"
          class="van-field-line"
          v-if="item.node=='file'"
          :formItem="item"
        ></van-field-fileupload>
      </div>
      <div class="buttonview" v-if="!typeno">
        <van-button size="small" round block type="info" native-type="submit">保存修改</van-button>
        <van-button size="small" round block type="info" @click.native="deleteclick">删除</van-button>
        <van-button size="small" round block type="info" native-type="submit">确认提交</van-button>
      </div>
      <div class="buttonview" v-if="isnew">
        <van-button size="small" round block type="info" native-type="submit">提交信息</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
/**
 * 表单根据配置条件，进行组件渲染。
 */
import Vue from "vue";
import { Form, Empty , Dialog , Toast } from "vant";

Vue.use(Form);
Vue.use(Empty);

import vanFieldText from "./template/van-field-text";
import vanFieldSelect from "./template/van-field-select";
import vanFieldCheckbox from "./template/van-field-checkbox";
import vanFieldRadio from "./template/van-field-radio";
import vanFieldDatetime from "./template/van-field-datetime";
import vanFieldCalendar from "./template/van-field-calendar";
import vanFieldFileupload from "./template/van-field-fileupload";

import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations, mapActions } = createNamespacedHelpers(
  "enterpriseinfo"
);
export default {
  name: "formRender",
  data() {
    return {
      typeno: true,
      isnew: false,
      cid:null
    };
  },
  props: {
    form: {
      type: Array,
      default: []
    },
    show: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    ...mapState({
      updateCorpStatus: state => state.updateCorpStatus
    })
  },
  created() {
    let a = this.$route.params.form
    this.cid = a[0].value
    let type = Number(this.$route.query.type);
    if (type == 0) {
      this.typeno = false;
    }
    if (type == -1) {
      this.isnew = true;
    }
  },
  components: {
    vanFieldText,
    vanFieldSelect,
    vanFieldCheckbox,
    vanFieldRadio,
    vanFieldDatetime,
    vanFieldCalendar,
    vanFieldFileupload
  },
  methods: {
    ...mapActions(["actAddCorp", "actUpdateCorpData","artdelete"]),
    ...mapMutations(["mutUpdateCorpStatus","mutDeleteid","mutClearDeleteid"]),
    deleteclick() {
      let _vm = this
      Dialog.confirm({
        title: "是否删除",
        message: "是否删除当前公司?"
      })
        .then(() => {
          _vm.mutDeleteid(this.cid)
          _vm.artdelete()
           Toast(this.updateCorpStatus.text);
           if (this.updateCorpStatus.code == 200) {
             this.$router.go(-1);
           }
        })
        .catch((err) => {
          console.log("错误",err)
          // on cancel
        });
    },
    onSubmit(values) {
      //提交表单数据
      this.$emit("submit", this.filterVal(values));
    },
    filterVal(values) {
      //数据过滤
      let obj = {};
      for (let item in values) {
        if (item !== "undefined" && typeof values[item] == "string") {
          obj[item] = values[item].replace(/\s+/g, "");
        } else if (item !== "undefined") {
          obj[item] = values[item];
        }
      }
      return obj;
    }
  }
};
</script>
<style lang="less" scoped>
.van-field-line .van-cell::after {
  content: "";
  position: absolute;
  pointer-events: none;
  right: 0;
  bottom: 0;
  left: 16px;
  border-bottom: 1px solid #ebedf0;
  transform: scaleY(0.5);
  box-sizing: border-box;
}
.hide-box {
  display: none;
}

.buttonview {
  margin: 16px;
  display: flex;
}
</style>
